﻿using com.guojin.mj.net.message.login;
using OneP.InfinityScrollView;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ClubItem : InfinityBaseItem
{
    public Text name, clubname, id, allnumber, number, gonggao, dengji, type;
    public RawImage head;
    private int Mytype;
    private string ThisID;
    private int GetRanking;//当前位置
    public Text tisi;
    public override void Reload(InfinityScrollView _infinity, int _index)
    {
        GetRanking = _index;
        if (SaveDate.GetInstance().AllClub.Clubs[_index].Type == 0)
        {
            type.text = "我的俱乐部";
            GameObject Club = GameObject.Find("MyClub(Clone)");
            if (Club)
            {
                Club.transform.GetChild(0).GetChild(1).gameObject.SetActive(false);
                Club.transform.GetChild(0).GetChild(2).gameObject.SetActive(true);
                Club.transform.GetChild(0).GetChild(2).GetChild(1).gameObject.SetActive(true);
                Club.GetComponent<MyClubbPanel>().MyClubId = SaveDate.GetInstance().AllClub.Clubs[_index].ClubId;
            }
        }
        else if (SaveDate.GetInstance().AllClub.Clubs[_index].Type == 2)//管理员
        {
            type.text = "我管理的俱乐部";
        }
        Mytype = SaveDate.GetInstance().AllClub.Clubs[_index].Type;
        name.text = SaveDate.GetInstance().AllClub.Clubs[_index].CreateName;
        clubname.text = SaveDate.GetInstance().AllClub.Clubs[_index].ClubName;
        id.text = SaveDate.GetInstance().AllClub.Clubs[_index].ClubId.ToString();
        int numbers = SaveDate.GetInstance().AllClub.Clubs[_index].NumPeople;
        allnumber.text = ThisID = numbers.ToString();
        number.text = SaveDate.GetInstance().AllClub.Clubs[_index].NowPeople.ToString();
        gonggao.text = SaveDate.GetInstance().AllClub.Clubs[_index].Clubcontent;
        if (numbers == 100)
        {
            dengji.text = "初级";
        }
        else if (numbers == 300)
        {
            dengji.text = "中级";
        }
        else
        {
            dengji.text = "高级";
        }
        if (SaveDate.GetInstance().AllClub.Clubs[_index].Money)
        {
            tisi.gameObject.SetActive(true);
        }
        DownloadHeadTexture(head, SaveDate.GetInstance().AllClub.Clubs[_index].Address);
        Debug.Log(SaveDate.GetInstance().AllClub.Clubs[_index].Address);
    }
    public void ShowNum()
    {
        if (Mytype == 1)
        {
            MessageManager.GetInstance().SendMsg773(id.text);
            SaveDate.GetInstance().SeeClubId = id.text;
        }
        else if (Mytype == 0)
        {
            SaveDate.GetInstance().NUmbers = number;
            UIManager.ChangeUI(UIManager.PageState.IfMyClub, (GameObject obj) =>
            {
                obj.GetComponent<ThisMyClub>().SetPage(SaveDate.GetInstance().AllClub.Clubs[GetRanking], 0);
            });
            SaveDate.GetInstance().SeeClubId = id.text;
        }
        else
        {
            SaveDate.GetInstance().NUmbers = number;
            UIManager.ChangeUI(UIManager.PageState.IfMyClub, (GameObject obj) =>
            {
                obj.GetComponent<ThisMyClub>().SetPage(SaveDate.GetInstance().AllClub.Clubs[GetRanking], 2);
            });
            SaveDate.GetInstance().SeeClubId = id.text;
        }
    }
    protected void DownloadHeadTexture(RawImage texture, string url)            //下载头像图片
    {
        //texture = null;
#if WX_SDK
           if (url != null)
           {
            //用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空
            //url = url.Substring(0, url.Length - 1);
            //url += "132";    
            StartCoroutine(DownloadTexture(texture, url));     
           }
           else
           {
              Debug.Log("下载头像Texture收到的url为空！");     
           }
#else
        url = URLConst.DefaultUserHeadUrl;
        StartCoroutine(DownloadTexture(texture, url));
#endif
    }

    private IEnumerator DownloadTexture(RawImage texture, string url)
    {
        WWW www = new WWW(url);
        yield return www;
        texture.texture = www.texture;
    }
}